package com.FengChaoSystem.servlet;

import com.FengChaoSystem.pojo.Coupon;
import com.FengChaoSystem.pojo.New;
import com.FengChaoSystem.service.CouponImpl;
import com.FengChaoSystem.service.CouponService.CouponService;
import com.FengChaoSystem.service.NewImpl;
import com.FengChaoSystem.service.NewService.NewService;
import com.FengChaoSystem.untils.CheckCodeUtil;
import com.FengChaoSystem.untils.VCodeUtils;
import com.alibaba.fastjson.JSON;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@WebServlet("/Coupon/*")
public class CouponServlet extends BaseServlet{
    private CouponImpl service=new CouponService();
    private static String onlyId=null;
    public void hasCoupon(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        int userId = (int) session.getAttribute("userId");
        Coupon coupon=new Coupon();
        coupon.setUserId(userId);
        List<Coupon> result = service.hasCoupon(coupon);
        if(result==null|| result.isEmpty()){
            response.getWriter().write("no");
        }else {
            onlyId=result.get(0).getOnlyId();
            String jsonString = JSON.toJSONString(onlyId);
            response.setContentType("text/json;charset=utf-8");
            response.getWriter().write(jsonString);
        }
    }
    public void addCoupon(HttpServletRequest request, HttpServletResponse response) throws Exception {

        HttpSession session = request.getSession();
        int userId = (int) session.getAttribute("userId");
        String onlyId = request.getParameter("onlyId");
        Coupon coupon=new Coupon();
        coupon.setFromUserId(userId);
        coupon.setOnlyId(onlyId);
        List<Coupon> coupons = service.hasCouponByOnlyId(coupon);
        if(coupons==null||coupons.isEmpty()){
            response.getWriter().write("error");
            return;
        }
        Coupon result = service.hasAdded(coupon);
        if(result!=null){
            response.getWriter().write("hasAdded");
            return;
        }
        int toUserId=coupons.get(0).getUserId();
        System.out.println(toUserId);
        System.out.println(coupon.toString());
        for (Coupon coupon1 : coupons) {
            System.out.println(coupon1.toString());
        }
        coupon.setStatus(0);
        coupon.setMoney(10);
        coupon.setUserId(toUserId);
        service.addCoupon(coupon);
        response.getWriter().write("success");
    }
    public void selectCountOfCoupon(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        int userId = (int) session.getAttribute("userId");
        Coupon coupon=new Coupon();
        coupon.setUserId(userId);
        List<Coupon> result = service.selectAllCanUseCoupon(coupon);
        String jsonString = JSON.toJSONString(result.size());
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }
    public void generateCoupon(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        int userId = (int) session.getAttribute("userId");
        Coupon coupon=new Coupon();
        coupon.setUserId(userId);
        coupon.setMoney(10);
        String newOnlyId=VCodeUtils.verifyCode(12);
        coupon.setOnlyId(newOnlyId);
        coupon.setFromUserId(userId);
         service.addCoupon(coupon);
        String jsonString = JSON.toJSONString(newOnlyId);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    }
